<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/11
 * Time: 13:56
 */

namespace app\admin\controller\comment;


use app\admin\controller\BaseController;
use app\admin\model\GoodsComment;
use think\Session;

class Goods extends BaseController
{
    public function page(){
        $goodsComment=new GoodsComment();
        $user=(new Session())->get("user");
        if($user["scope"]==0){//普通管理员

        }else{

        }
        $items=$goodsComment->where([
            "is_del"        =>0
        ])->with(["goods"])->select();
        $this->assign("items",$items);
        return $this->fetch("index");
    }

    public function del(){
        $ids=[];
        $id=$this->request->param("id");
        $goodsComment=new GoodsComment();
        if(!is_array($id)){
            $ids[]=$id;
        }else{
            $ids=$id;
        }
        //dump($id);die();
        foreach ($ids as $key =>$value){
            $res=$goodsComment::update([
                "id"        =>$value,
                "is_del"    =>1
            ]);
        }

        if($res){
            return json([
                "code"      =>200,
                "msg"       =>"success"
            ]);
        }
    }

    public function edit(){
        $goodsComment=new GoodsComment();
        if($this->request->isGet()){
            if($this->request->has("id")){//编辑
                $id=$this->request->get("id");
                $item=$goodsComment->find([
                    "id"    =>$id
                ]);
                $this->assign("item",$item);
            }else{//添加

            }
            $goods=new \app\admin\model\Goods();
            $goodsItems=$goods->select();
            $this->assign("goods",$goodsItems);
            return $this->fetch();
        }else{
            $data=$this->request->param();
            //dump($data);die();
            if(!empty($data["id"])){//更新
                $goodsComment::update($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"update success"
                ]);
            }else{//插入
                unset($data["id"]);
                $goodsComment->save($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"insert success"
                ]);
            }
        }

    }

}